﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.Bliss.Services;
using com.Bliss;

public partial class Controls_SendMessage : UserControlBase
{
    public int ToUserID
    {
        get
        {
            int ToUserID = 0;
            if (Request.QueryString["ToId"] != null)
            {
                int.TryParse(Request.QueryString["ToId"].ToString(), out ToUserID);
            }
            return ToUserID;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            errorDiv.Visible = false;
            maintable.Visible = true;
            hdnToUserId.Value = ToUserID.ToString();
            if (ToUserID == 0)
            {
                errorDiv.Visible = true;
                maintable.Visible = false;
                // Code to restrict
            }
            else if (ToUserID == UserID.Value)
            {
                errorDiv.Visible = true;
                maintable.Visible = false;
            }
            else
            {
                IUserService service = new UserService();
                var userService = service.GetUser(ToUserID);
                com.Bliss.User user = userService.Data;
                if (user == null)
                {
                    errorDiv.Visible = true;
                    maintable.Visible = false;
                    // Code to restrict
                }
                else
                {
                    string ImageName = user.ImageName;
                    if (ImageName == "NoImage")
                    {
                        imgToUser.ImageUrl = ResolveUrl(@"~/UserImage/image_missing.jpg");
                    }
                    else
                    {
                        imgToUser.ImageUrl = ResolveUrl("~/UserImage/" + ImageName);
                    }
                    imgToUser.PostBackUrl = ResolveUrl("~/UserDetails.aspx?Id=" + user.ID.ToString());
                    lnkToUserName.Text = user.Name;
                    lnkToUserName.PostBackUrl = ResolveUrl("~/UserDetails.aspx?Id=" + user.ID.ToString());
                }
            }

        }
    }
    protected void ButtonSend_Click(object sender, EventArgs e)
    {
        string message = TextBoxScrap.Text;
        IPostService service = new PostService();
        int ToId = 0;
        int.TryParse(hdnToUserId.Value, out ToId);
        if (ToId != 0)
        {
            service.SendPrivateMessage(UserID.Value, null, ToId, message);
            Response.Redirect("~/PrivateMessages.aspx");
        }
        else
        {
            errorDiv.Visible = true ;
            maintable.Visible = false;
            // Code to restrict
        }

    }
}